<template>
    <div class="mod-menu">
        <el-form :inline="true" :model="searchData" @keyup.enter.native="getDataList()">
            <el-form-item>
                <el-input v-model="searchData.userName" placeholder="姓名" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="searchData.gender" placeholder="性别" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="searchData.contactNumber" placeholder="电话号码" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="getDataList()" type="primary">查询</el-button>
                <el-button v-if="isAuth('operate:building:add')" type="primary" @click="addOrUpdateHandle()"
                    >新增</el-button
                >
            </el-form-item>
        </el-form>
        <el-table :data="dataList" border style="width: 100%">
            <el-table-column header-align="center" align="center" min-width="20%" label="居民id" prop="id">
            </el-table-column>
            <el-table-column header-align="center" align="center" min-width="20%" label="姓名" prop="userName">
            </el-table-column>
            <el-table-column header-align="center" align="center" min-width="20%" label="门牌号" prop="doorNumber">
            </el-table-column>
            <el-table-column header-align="center" align="center" min-width="10%" label="性别" prop="gender">
            </el-table-column>
            <el-table-column header-align="center" align="center" min-width="40%" label="身份证" prop="idNumber">
            </el-table-column>
            <el-table-column header-align="center" align="center" min-width="20%" label="与户主关系" prop="relationshipToOwner">
            </el-table-column>
            <el-table-column header-align="center" align="center" min-width="20%" label="联系号码" prop="contactNumber">
            </el-table-column>
            <el-table-column
                prop="type"
                header-align="center"
                align="center"
                min-width="20%"
                label="工作类型"
            >
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.type == 7" size="medium" type="success">技术人员</el-tag>
                    <el-tag v-if="scope.row.type == 8" size="medium" type="success">教师</el-tag>
                    <el-tag v-if="scope.row.type == 9" size="medium" type="success">务工人员</el-tag>
                    <el-tag v-if="scope.row.type == 10" size="medium" type="success">退休人员</el-tag>
                    <el-tag v-if="scope.row.type == 11" size="medium" type="success">困难户</el-tag>
                </template>
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" label="操作">
                <template slot-scope="scope">
                    <el-button
                        round
                        v-if="isAuth('operate:people:update')"
                        type="primary"
                        size="mini"
                        @click="showinfor(scope.row.id)"
                        >修改</el-button
                    >
                    <el-button
                        round
                        v-if="isAuth('operate:people:delete')"
                        type="danger"
                        size="mini"
                        @click="deleteHandle(scope.row.id)"
                        >删除</el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <!-- <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination> -->
        <!-- 弹窗, 新增 / 修改 -->
        <showPeopleInfo
            v-if="showPeopleInfo"
            ref="showPeopleInfo"
            @refreshDataList="getDataList"
        ></showPeopleInfo>
        <AddOrUpdate v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></AddOrUpdate>
    </div>
</template>

<script>
import showPeopleInfo from './people-info';
import AddOrUpdate from './people-info-add';
export default {
    data() {
        return {
            dataForm: {
                "id": null,
                "buildingId": 0,
                "doorNumber": "",
                "relationshipToOwner": "",
                "gender": "",
                "idNumber": "",
                "contactNumber": "",
                "type": "",
                userName: ''
            },
            dataList: [
                {
                    "id": null,
                    "buildingId": 0,
                    "doorNumber": "",
                    "relationshipToOwner": "",
                    "gender": "",
                    "idNumber": "",
                    "contactNumber": "",
                    "type": "",
                    userName: ''
                }
            ],
            searchData: {
                userName: '',
                gender: '',
                contactNumber: ''
            },
            dataListLoading: false,
            addOrUpdateVisible: false,
            showPeopleInfo: false,
            pageIndex: 1,
            pageSize: 50,
            totalPage: 0,
            buildingId: 0,
        };
    },
    components: {
        showPeopleInfo,
        AddOrUpdate
    },
    activated() {
        if(this.$route.query){
            this.buildingId = this.$route.query.id;
        }
        this.getDataList();
    },
    methods: {
        // 获取数据列表
        getDataList() {
            this.dataListLoading = true;
            let data = {
                page: this.pageIndex,
                limit: this.pageSize,
            }
            if(this.searchData.userName){
                data.userName = this.searchData.userName;
            }
            if(this.searchData.gender){
                data.gender = this.searchData.gender;
            }
            if(this.searchData.contactNumber){
                data.contactNumber = this.searchData.contactNumber;
            }
            if(this.buildingId){
                data.buildingId = this.buildingId;
            }
            this.$http({
                url: this.$http.adornUrl('/people/selectAll'),
                method: 'post',
                data: this.$http.adornData(data),
            }).then(({ data }) => {
                this.dataList = data;
                this.dataListLoading = false;
            });
        },
        showinfor(id) {
            this.dataListLoading = true;
            this.$http({
                url: this.$http.adornUrl(`/people/selectAllPeopleById`),
                method: 'post',
                data: this.$http.adornData({
                    id: id
                }),
            }).then(({ data }) => {
                this.dataForm = data;
                this.dataListLoading = false;
                this.showPeopleInfo = true;
                this.$nextTick(() => {
                    this.$refs.showPeopleInfo.init(this.dataForm);
                });
            });
        },
        // 新增 / 修改
        addOrUpdateHandle() {
            this.addOrUpdateVisible = true;
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init();
            });
        },
        // 每页数
        sizeChangeHandle(val) {
            this.pageSize = val;
            this.pageIndex = 1;
            this.getDataList();
        },
        // 当前页
        currentChangeHandle(val) {
            this.pageIndex = val;
            this.getDataList();
        },
        // 删除
        deleteHandle(id) {
            var ids = new Array();
            ids[0] = id;
            this.$confirm(`确定对其进行【删除】操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.$http({
                        url: this.$http.adornUrl(`/people/removePeople`),
                        method: 'post',
                        data: this.$http.adornData({id}),
                    }).then(({ data }) => {
                        if (data && data.code === 0) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.getDataList();
                                },
                            });
                        } else {
                            this.$message.error(data.msg);
                        }
                    });
                })
                .catch(() => {});
        },
    },
};
</script>
